<?php
namespace Kuoqi\Html\Page;
class Row{

    public $codes = array();//保存的代码片段

    public $format = 'text';//常用类型[code,text]
    public $stream = '';//代码输出流

    public function addItem($text = ''){
        $text = htmlspecialchars($text);
        $text = str_replace(' ','&nbsp;',$text);
        $this->codes[] = $text;
    }

    public function flushCode(){
        $code = '';
        foreach($this->codes as $k=>$item){
            $code .= sprintf('<span style="%sdisplay: block;">%s</span>'
                ,''
                ,$item);
        }
        $this->stream .= sprintf('<p style="line-height: 1.85;margin-top: 5px;margin-bottom: 5px;font-size: 15px;border: 1px solid #3a3a3a;box-sizing: border-box;padding: 5px 15px;font-family: serif;background-color: #666;color: #fff;padding-top: 10px;padding-bottom: 10px;border-radius: 5px;">%s</p>'
            ,$code);
    }

    public function flushTitle(){
        foreach($this->codes as $item){
            $this->stream .= sprintf('<p style="line-height: 1.85;margin-top: 5px;margin-bottom: 5px;font-size: 15px;font-weight: 600;">%s</p>',$item);
        }
    }

    public function flushLineLimit(){
        $this->stream .= '<p style="line-height: 1.85;margin-top: 5px;margin-bottom: 5px;background-color: #e9e9e9;height: 1px;"></p>';
    }

    public function flushText(){
        foreach($this->codes as $item){
            $this->stream .= sprintf('<p style="line-height: 1.85;margin-top: 5px;margin-bottom: 5px;font-size: 15px;">%s</p>',$item);
        }
    }

    public function flushTips(){
        foreach($this->codes as $item){
            $this->stream .= sprintf('<p style="line-height: 1.85;margin-top: 5px;margin-bottom: 5px;font-size: 13px;color: #999;">%s</p>',$item);
        }
    }

    public function flush(){
        $this->stream = '';
        switch($this->format){
            case 'code':$this->flushCode();break;
            case 'title':$this->flushTitle();break;
            case 'line-limit':$this->flushLineLimit();break;
            case 'tips':$this->flushTips();break;
            default:$this->flushText();
        }
        return $this->stream;
    }

}